<?php
// vbbot post-commit hook for vBulletin
// Posts a new post to a thread about new revisions in a Google Code project.
// by Cee Pee

/* Configuration */
$userid = 1; // User ID to post as
$forumid = 1;
$threadid = 1; // Thread ID to post in
$secretKey = ''; // Shared secret key from Google Code
$showsignature = true; // Show user's signature
$allowsmilie = false; // Allow smilie replacements
$posticon = 1; // Post icon

error_reporting(E_ALL & ~E_NOTICE);

define('THIS_SCRIPT', 'vbbot');
define('CSRF_PROTECTION', true);
define('LOCATION_BYPASS', 1);
define('NOPMPOPUP', 1);
define('SKIP_SESSIONCREATE', 1);
define('SKIP_USERINFO', 1);

$phrasegroups = array();
$specialtemplates = array();
$actiontemplates = array();

require_once('./global.php');
require_once(DIR . '/includes/functions_newpost.php');

function make_paths($project, $rev, $paths) {
	if ($paths) {
		$out = array();
		foreach ($paths as $path) {
			$out[] = "[url=http://code.google.com/p/{$project}/source/browse{$path}?r={$rev}]{$path}[/url]";
		}
		return implode(', ', $out);
	}
	else {
		return 'none';
	}
}

$data = file_get_contents('php://input');
$digest = hash_hmac('md5', $data, $secretKey);

if ( !empty($_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC']) &&
	$digest == $_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC']
) {
	// Authenticated
	$vbulletin->session->set_session_visibility(false);
	$vbulletin->userid = $userid;
	$vbulletin->userinfo =& fetch_userinfo($vbulletin->userid);

	$foruminfo =& fetch_foruminfo($forumid);
	$forumperms = fetch_permissions($forumid);
	$threadinfo =& fetch_threadinfo($threadid);

	$json = json_decode($data);

	$title = "Re: {$json->project_name} revisions";
	$message = "The following " . ($json->revision_count == 1 ? "revision was" : "{$json->revision_count} revisions were") . " committed to [url=http://code.google.com/p/{$json->project_name}/]{$json->project_name}[/url]:";
	foreach ( $json->revisions as $rev ) {
		$time = date(DATE_RFC822, $rev->timestamp);
		$url = "http://code.google.com/p/{$json->project_name}/source/detail?r={$rev->revision}";

		$added = make_paths($json->project_name, $rev->revision, $rev->added);
		$modified = make_paths($json->project_name, $rev->revision, $rev->modified);
		$removed = make_paths($json->project_name, $rev->revision, $rev->removed);

		$log = "[c]";
		if ( strpos($rev->message, "\n") !== false ) {
			$log .= "\n";
		}
		$log .= $rev->message;
		if ( strlen($rev->message) >= 500 ) {
			$log .= " [...]";
		}
		$log .= "[/c]";

		$message .= <<<EOT
[list][*]Revision: {$rev->revision}
[*]Author: {$rev->author}
[*]Date: {$time}
[*]Message: {$log}
[*]URL: [url]{$url}[/url]
[*]Changed paths: {$rev->path_count} [list]
  [*]Added: {$added}
  [*]Modified: {$modified}
  [*]Removed: {$removed}
[/list]
[/list]
EOT;
	}

	$dataman =& datamanager_init('Post', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
	$sig = true;
	$smilie = false;
	$icon = 1;

	// set info
	$dataman->set_info('preview', false);
	$dataman->set_info('parseurl', true);
	$dataman->set_info('posthash', md5($newpost['poststarttime'] . $vbulletin->userid . $vbulletin->userinfo['salt']));
	$dataman->set_info('forum', $foruminfo);
	$dataman->set_info('thread', $threadinfo);

	// set options
	$dataman->setr('showsignature', $sig);
	$dataman->setr('allowsmilie', $smilie);

	// set data
	$dataman->setr('userid', $vbulletin->userinfo['userid']);
	$dataman->setr('title', $title);
	$dataman->setr('pagetext', $message);
	$dataman->setr('iconid', $icon);
	$dataman->set('visible', 1);

	// get parentid of the new post
	$getfirstpost = $vbulletin->db->query_first("SELECT postid FROM " . TABLE_PREFIX . "post WHERE threadid=$threadinfo[threadid] ORDER BY dateline LIMIT 1");
	$parentid = $getfirstpost['postid'];
	$dataman->setr('parentid', $parentid);
	$dataman->setr('threadid', $threadinfo['threadid']);

	$dataman->pre_save();
	$errors = array_merge($errors, $dataman->errors);

	if ( count($errors) > 0 ) {
		header('HTTP/1.1 500 Internal Server Error');
		echo implode("\n", $errors);
	}
	else {
		$id = $dataman->save();
		header('HTTP/1.1 200 OK');
		echo "Post id: $id\n";
	}
}
else {
	header('HTTP/1.1 403 Forbidden');
	echo "Go away.\n";
}
die();
